package com.itqian.controller.seller;

import com.itqian.common.exception.Assert;
import com.itqian.common.exception.ResponseEnum;
import com.itqian.common.utils.ResultVOUtil;
import com.itqian.common.vo.ResultVO;
import com.itqian.seller.entity.FoodInfo;
import com.itqian.seller.form.FoodInfoForm;
import com.itqian.seller.mapper.FoodInfoMapper;
import com.itqian.seller.service.FoodInfoService;
import com.itqian.system.entity.Constants;
import com.itqian.utils.TokenUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

/**
 * @author shishuqian
 * date 2022/3/22
 * time 12:26
 **/
@RestController
@RequestMapping("/seller/foodInfo")
public class FoodInfoController {

    @Autowired
    private FoodInfoService foodInfoService;

    @Autowired
    private FoodInfoMapper foodInfoMapper;

    /**
     *    分页查看此商铺的所有菜品
     */
    @GetMapping("/list/{page}/{size}")
    public ResultVO list(@RequestHeader("token") String token, @PathVariable Integer page, @PathVariable Integer size){
        //参数非空校验
        Assert.notNull(page, ResponseEnum.PAGE_NOTNULL);
        Assert.notNull(size,ResponseEnum.SIZE_NOTNULL);

        //获取店铺id
        Integer storeId = TokenUtils.getStoreId(token);

        Map<String, Object> list = foodInfoService.list(storeId, page, size);
        return ResultVOUtil.success(list);
    }

    /**
     *    根据菜品类别分页查看此商铺的所有菜品
     */
    @GetMapping("/findAllByCategory/{categoryId}/{page}/{size}")
    public ResultVO findAllByCategory(@PathVariable Integer categoryId,
                                      @PathVariable Integer page,
                                      @PathVariable Integer size){
        //参数非空校验
        Assert.notNull(categoryId,ResponseEnum.PARAM_NOTNULL);
        Assert.notNull(page, ResponseEnum.PAGE_NOTNULL);
        Assert.notNull(size,ResponseEnum.SIZE_NOTNULL);

        Map<String, Object> map = foodInfoService.findAllByCategory(categoryId, page, size);
        return ResultVOUtil.success(map);
    }

    /**
     *    添加或修改菜品
     */
    @PostMapping("/saveOrUpdate")
    public ResultVO saveOrUpdate(@RequestHeader("token") String token,
                                 @Valid @RequestBody FoodInfoForm foodInfoForm){
        //获取店铺id
        Integer storeId = TokenUtils.getStoreId(token);

        FoodInfo foodInfo = new FoodInfo();
        BeanUtils.copyProperties(foodInfoForm,foodInfo);
        foodInfo.setStoreId(storeId);
        //添加或修改
        boolean b = foodInfoService.saveOrUpdate(foodInfo);
        if (!b){
            return ResultVOUtil.fail("操作失败，请重试...");
        }
        return ResultVOUtil.success(null);
    }

    /**
     *    上架/下架菜品
     */
    @PutMapping("/status/{foodId}")
    public ResultVO updateStatus(@PathVariable Integer foodId){

        FoodInfo foodInfo = foodInfoService.getById(foodId);
        foodInfo.setFoodStatus(!foodInfo.getFoodStatus());
        boolean b = foodInfoService.updateById(foodInfo);
        if (!b){
            return ResultVOUtil.fail("修改菜品状态失败，请重试...");
        }
        return ResultVOUtil.success(null);
    }

    /**
     *    促销/不促销菜品
     */
    @PutMapping("/promotion/{foodId}")
    public ResultVO promotion(@RequestHeader("token") String token,@PathVariable Integer foodId){
        //获取店铺id
        Integer storeId = TokenUtils.getStoreId(token);

        FoodInfo foodInfo = foodInfoService.getById(foodId);
        //如果当前菜品促销状态为false,说明现在的操作是促销，需要判断促销的菜品数量是否大于限定值
        if(!foodInfo.getPromotion()){
            int i = foodInfoMapper.promotionNum(storeId);
            if (i >= Constants.PROMOTION_NUM){
                return ResultVOUtil.fail("同时促销菜品的数量最大为10个");
            }
        }
        //改变促销状态
        foodInfo.setPromotion(!foodInfo.getPromotion());

        boolean b = foodInfoService.updateById(foodInfo);
        if (!b){
            return ResultVOUtil.fail("促销菜品失败，请重试...");
        }
        return ResultVOUtil.success(null);
    }


    /**
     *    删除菜品
     */
    @DeleteMapping("/delete/{foodId}")
    public ResultVO delete(@PathVariable Integer foodId){
        //参数非空校验
        Assert.notNull(foodId,ResponseEnum.PARAM_NOTNULL);

        boolean b = foodInfoService.removeById(foodId);
        if (!b){
            return ResultVOUtil.fail("该菜品已删除");
        }
        return ResultVOUtil.success(null);
    }




}
